Zepto(function($){
	setCntH();
	$(window).on('resize', setCntH);
});

function addTap() {
	var $lnk = $('[data-url]');
	
	$lnk.tap(function () {
		window.location.href = $(this).data('url');
	});
}

function setCntH() {
	var cnt = $('.content').first();
	
	if ($(document).height() < $(window).height()) {
		cnt.height(cnt.height() + ($(window).height() - $(document).height()) - 1);
	}
}

function maxZ(obj) {
	var elms = (obj || document).getElementsByTagName('*'), z = 0, arr = [];
	
	for (var i = 0, len = elms.length; i < len; i++) {
		var z = parseInt(setStyle(elms[i], 'z-index'));
		arr.push(isNaN(z) ? 0 : z);
	}
	
	return Math.max.apply(null, arr);
}

function setStyle(obj, attr, value) {
	if (value) return obj.style[attr] = value;

	var attribute = attr.replace(/-(\w)/ig, function ($1, $2) {
		return ($2 + '').toUpperCase();
	});

	if (obj.currentStyle) {
		return obj.currentStyle[attribute];    
	} else {
		return getComputedStyle(obj, false)[attribute];    
	}
}

function Popup(popup) {
	var oPop = popup || document.querySelector('.popup');
	var oInner = oPop.querySelector('.popup_i');
	var oMask = document.querySelector('.mask');
	var h = document.documentElement.clientHeight;
	var timer = null;
	var z = maxZ();
	
	oPop.style.display = 'block';
	oPop.style.zIndex = z + 2;
	
	oInner.style.WebkitTransform = 'translateY(' + h + 'px)';
	oInner.style.WebkitTransition = '.5s all ease';
	
	oMask.style.backgroundColor = 'rgba(0, 0, 0, 0)';
	oMask.style.display = 'block';
	oMask.style.WebkitTransition = '.5s all ease';
	oMask.style.zIndex = z + 1;
	
	timer = setTimeout(function () {
		oMask.style.backgroundColor = 'rgba(0, 0, 0, .6)';
	  oInner.style.WebkitTransform = 'translateY(0)';
		
		oInner.addEventListener('webkitTransitionEnd', function () {
			oInner.style.WebkitTransition = 'none';
		}, false);
		clearTimeout(timer);
	}, 0);
	
	var oClose = oPop.querySelectorAll('.close')[0];
	oClose.onclick = function () {
		closePop(oPop, oMask);
	};
	
	oClose.addEventListener('touchend', function (ev) {
		closePop(oPop, oMask);
		ev.preventDefault();
	}, false);
	
	oInner.addEventListener('touchend', function (ev) {
		var target = ev.srcElement;
		
		if (target.classList.contains('content')) {
			closePop(oPop, oMask);
		}
		
		ev.preventDefault();
	}, false);
}

function closePop(popup, mask) {
	var oInner = popup.querySelector('.popup_i');
	var h = document.documentElement.clientHeight;
  var mask = document.querySelector('.mask');
	
	oInner.style.WebkitTransition = '.5s all ease';
	oInner.style.WebkitTransform = 'translateY(' + h + 'px)';
	
	mask.style.WebkitTransition = '1s all ease';
	mask.style.backgroundColor = 'rgba(0, 0, 0, 0)';
	
	var timer = setTimeout(function () {
	  popup.style.display = 'none';
		popup.style.zIndex = 0;
		mask.style.display = 'none';
		mask.style.zIndex = 0;
	}, 500);
}

function Alert() {
	this.obj = null;  // 弹层
	this.btnClose = null;  // 关闭按钮
	this.btnSure = null;  // 确定按钮
	this.mask = true;  // 是否显示遮罩层
	this.timer = null;  // 定时器
	this.times = 0;  //  弹层显示时间
	this.fix = false;  // 是否固定定位
	this.remove = false;  // 关闭后是否移除
	this.tit = '';  // 提示标题
	this.tip = '';  // 提示文字
	this.foot = true;  // 是否显示底部
	this.elmTip = null;
	this.html = '';
	this.z = 0;
	this.l = 0;
	this.t = 0;
	this.parent = null;
}

Alert.prototype.init = function (opt) {
	var self = this;
	
	copy(this, opt);
	
	if (!this.obj) {
		this.obj = document.createElement('div');
		dui.klass(this.obj, 'com_pop');
		if (this.id) this.obj.id = this.id;
		
		this.html = '<div class="com_pop_cont"><div class="com_pop_tit">提示</div><div class="com_pop_txt">提示文字</div></div><div class="com_pop_foot"><a class="com_btn com_btn_4 btn_close" href="javascript:;">关闭</a></div>';
		
		this.obj.innerHTML = this.html;

		document.body.appendChild(this.obj);
	}
	
	this.parent = this.obj.parentNode;
	
	this.z = dui.maxZ();
	
	if (this.mask) this.mask = document.createElement('div');
	
	if (!this.btnClose) this.btnClose = dui.$('.btn_close:eq(0)', this.obj);

	if (this.mask) {
	  dui.klass(this.mask, 'com_mask');
	  dui.append(this.parent, this.mask);
	  dui.insertAfter(this.parent, this.obj, this.mask);
	}
	
	this.elmTit = dui.$('.com_pop_tit:eq(0)', this.obj);
	
	if (this.tit) {
		dui.html(this.elmTit, this.tit);
	} else {
		dui.hide(this.elmTit);
	}
	
	if (this.titAddClass) {
		this.elmTit.classList.add(this.titAddClass);
	}
	
	if (this.tip) {
		this.elmTip = dui.$('.com_pop_txt:eq(0)', this.obj);
		dui.html(this.elmTip, this.tip);
	}
	
	if (this.tipAddClass) {
		this.elmTip.classList.add(this.tipAddClass);
	}
	
	if (!this.foot) {
		this.foot = dui.$('.com_pop_foot:eq(0)', this.obj);
		dui.hide(this.foot);
	}

	/*dui.bind(this.btnClose, 'click', function () {
		self.fnExit.call(self);
	});*/
	
	this.btnClose && (this.btnClose.onclick = function () {
		self.fnExit.call(self);
	});
	
	this.setPos();
	
	dui.show(this.obj);
	if (this.mask) dui.show(this.mask);
	dui.bind(window, 'resize', this.setPos);
	
	if (this.times) {
	  this.timer = setTimeout(function () {
			self.fnExit.call(self);
			clearTimeout(self.timer);
		}, this.times);
	}
};

Alert.prototype.fnExit = function () {
	dui.hide(this.obj);
	if (this.mask) dui.hide(this.mask);
	
	this.fnClose && this.fnClose();
	
	if (this.remove) {
		this.parent.removeChild(this.obj);
	}
	
	if (this.mask) this.parent.removeChild(this.mask);
};

Alert.prototype.setPos = function () {
	if (this.mask) {
	  dui.css(this.mask, {
		  'height': dui.documentHeight() + 'px',
		  'z-index': this.z + 1
	  });
	}

	this.t = Math.round((dui.viewHeight() - dui.outerHeight(this.obj)) / 2) + (this.fix ? 0 : dui.scrollTop());
	this.t < 0 ? 0 : this.t;
	
	this.l =  Math.round((dui.viewWidth() - dui.outerWidth(this.obj)) / 2);
	this.l < 0 ? 0 : this.l;

	dui.css(this.obj, {
		'position': this.fix ? 'fixed' : 'absolute',
		'z-index': this.z + 2,
		'top': this.t + 'px',
		'left': this.l + 'px'
	});
};

function copy (obj1, obj2) {
	for (var attr in obj2) if (obj2.hasOwnProperty(attr)) obj1[attr] = obj2[attr];
}

function toDouble(n) {
	return n < 10 ? ('0' + n) : n;
}